package com.trafficsys.modules.licenseplaterecognition.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.trafficsys.modules.entity.Ticket;
import com.trafficsys.modules.licenseplaterecognition.service.TicketService;
import com.trafficsys.modules.licenseplaterecognition.vo.TicketVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/api")
public class TicketController {
    @Autowired
    private TicketService ticketService;

    /**
     * 127.0.0.1:8080/api/ocr
     *
     * @param img
     * @return
     */
    @PostMapping(value = "/ocr")
    public HashMap<String, Object> imgOCR(String img) {
        return ticketService.carNoOCR(img);
    }

    /**
     * 127.0.0.1:8080/api/create
     * {"carNo":"川A7895","policeId":"1","ruleId":"1"}
     *
     * @param ticket
     * @return
     */
    @PostMapping(value = "/create", consumes = "application/json")
    public HashMap<String, Object> createTicket(@RequestBody Ticket ticket) {
        return ticketService.addTicket(ticket);
    }

    /**
     * 127.0.0.1:8080/api/allTickets?current=1&size=1
     */
    @GetMapping(value = "/allTickets")
    public IPage<TicketVo> showAllTickets(@RequestParam("current") int current, @RequestParam("size") int size) {
        Page<TicketVo> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        return ticketService.allTickets(page);
    }

    /**
     * 127.0.0.1:8080/api/allTickets?current=1&size=5&id=2
     */
    @GetMapping(value = "/selfTickets")
    public IPage<TicketVo> showSelfTickets(@RequestParam("current") int current, @RequestParam("size") int size, @RequestParam("card_id") String card_id) {
        Page<TicketVo> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        return ticketService.getSelfTickets(page, card_id);
    }
}
